<?php
header('content-type:text/html;charset=utf-8');
// 连接数据库
$db = mysqli_connect('127.0.0.1','root','123456','test');
$sql = 'SELECT * FROM t_cate';
$exe_res = mysqli_query($db, $sql);
$data = mysqli_fetch_all($exe_res,MYSQLI_ASSOC);
// print_r($data);
// 循环 判断 递归
function getTree($arr,$pid=0,$level=1){
	static $list = [];
	foreach ($arr as $key => $value) {
		if($value['pid'] == $pid){
			$value['level'] = $level;
			$list[] = $value;
			unset($arr[$key]); // 删除已排好的数据，减少循环次数
			getTree($arr,$value['id'],$level+1);
		}
	}
	return $list;
}

$res = getTree($data);
echo '<pre/>';
print_r($res);

function get_child_list($data,$id,$pid){

}